﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ULinq.GenTools;
using System.IO;
using System.Data.Linq;
using ULinqGenTest.Ent;

namespace ULinqGenTest
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            ULinqCodeGenerator generator = new ULinqCodeGenerator(rbCSharp.Checked ? "CSharp" : "VB");
            string fileContent;
            using (FileStream fs = File.OpenRead(@"..\..\Northwind.dbml"))
            using (StreamReader rd = new StreamReader(fs))
            {
                fileContent = rd.ReadToEnd();
            }
            txtResult.Text = generator.BuildCode("Northwind.dbml", fileContent);

        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            string connString = global::ULinqGenTest.Properties.Settings.Default.NorthwindConnectionString;
            using (NorthwindDC dc = new NorthwindDC(connString))
            {
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<Order>(o => o.Customer);

                dc.LoadOptions = dlo;

                Table<Order> orders = dc.GetTable<Order>();
                var order1 = (from o in orders
                              where o.Order_Details.Count > 1
                              orderby o.OrderID
                              select o).FirstOrDefault();
                MessageBox.Show(order1.Customer.ContactName);
            }

            MessageBox.Show(VBClassLibrary.Test.NorthwindTest1());
        }
    }
}
